package cn.stronglink.assetmanage.business.inspection.model;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.PatrolDao;
import cn.stronglink.assetmanage.dao.PatrolDetailDao;
import cn.stronglink.assetmanage.entity.Patrol;
import cn.stronglink.assetmanage.entity.PatrolDetail;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Created by zhengjinglan on 2018/6/18.
 */

public class AlreadyPatrolModel {
    //1为未巡检 2为处理中 3为已巡检
    public List<Patrol> getDataByOrderNoDistince() {
        List<Patrol> patrolList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getPatrolDao().queryBuilder().distinct()
                .orderDesc(PatrolDao.Properties.Id_).
                        where(PatrolDao.Properties.Status.eq(Constants.STATUS_THREE)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (patrolList != null) {
            return patrolList;
        }
        return null;
    }

    /**
     * 根据表单获得二级列表
     * @param orderNo 表单号
     * @return
     */
    public Map<String, List<PatrolDetail>> getDataByOrderNo(String orderNo) {

        //获得二级列表
        Map<String, List<PatrolDetail>> inventoryList = new HashMap<String, List<PatrolDetail>>();
        List<PatrolDetail> patrolDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getPatrolDetailDao().queryBuilder().
                where(PatrolDetailDao.Properties.OrderNo.eq(orderNo)).
                where(PatrolDetailDao.Properties.Status.eq(Constants.STATUS_THREE)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.patrol_count),
                patrolDetailList);

        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }


    public Patrol getCheckByOrderNo(String orderNo) {
        Patrol patrol = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getPatrolDao().queryBuilder().distinct()
                .orderDesc(PatrolDao.Properties.Id_).
                        where(PatrolDao.Properties.Id_.eq(orderNo)).
                        where(PatrolDao.Properties.Status.eq(Constants.STATUS_THREE)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (patrol != null) {
            return patrol;
        }
        return null;
    }

    /**
     * 根据表单id获取资产列表
     *
     * @param id_
     * @return
     */
    public List<PatrolDetail> getDetailByOrderNo(String id_) {
        List<PatrolDetail> inventoryList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getPatrolDetailDao().queryBuilder().
                where(PatrolDetailDao.Properties.OrderNo.eq(id_)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    /**
     *通过表单号查找已巡检单
     * @param orderNo
     * @return
     */
    public Patrol getPatrolyOrderNo(String orderNo) {
        Patrol inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getPatrolDao().queryBuilder().distinct()
                .orderDesc(PatrolDao.Properties.Id_).
                        where(PatrolDao.Properties.Id_.eq(orderNo)).
                        where(PatrolDao.Properties.Status.eq(Constants.STATUS_THREE)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }

}
